package com.helasasa.maxfunds.lineofcredit.cashloan.ghana.screen.proof.subview

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.helasasa.maxfunds.lineofcredit.cashloan.R
import org.json.JSONArray
import org.json.JSONObject

interface IProofGhanaSelectListener {
    fun onClick(tid: String)
}

class ProofGhanaSelect : LinearLayout {

    private lateinit var dataList: MutableList<JSONObject>
    private lateinit var adapter: ProofGhanaSelectAdapter

    private lateinit var title: TextView
    private lateinit var input: EditText
    private lateinit var inputContainer: RelativeLayout
    private lateinit var click: View
    private lateinit var list: RecyclerView

    private var bindData: JSONObject? = null
    private var onItemClick: IProofGhanaSelectListener? = null

    constructor(context: Context) : super(context) {
        initializeView(context)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initializeView(context)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        initializeView(context)
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun initializeView(context: Context) {
        inflate(context, R.layout.view_ghana_proof_select, this)
        orientation = VERTICAL
        dataList = mutableListOf()

        title = findViewById(R.id.title)
        input = findViewById(R.id.proofSelect)
        list = findViewById(R.id.proofSelectList)
        click = findViewById(R.id.proofSelectClick)
        inputContainer = findViewById(R.id.inputContainer)

        adapter = ProofGhanaSelectAdapter(dataList, object : IProofGhanaSelectAdapterListener {
            override fun onClick(position: Int, item: JSONObject) {
                val inputText = item.optString("title")
                val inputValue = item.optString("value")
                setInputText(inputText)
                bindData?.put("content", inputText)
                bindData?.put("realContent", inputValue)
                list.visibility = GONE
                onItemClick?.onClick(bindData?.optString("paramName") ?: "")
            }
        })
        list.layoutManager = LinearLayoutManager(context)
        list.adapter = adapter

        click.setOnClickListener {
            if (list.visibility == View.VISIBLE) {
                list.visibility = View.GONE
            } else {
                list.visibility = View.VISIBLE
                setDataList(
                    this.bindData?.optJSONArray("values") ?: JSONArray(),
                    input.text.toString()
                )
                adapter.notifyDataSetChanged()
            }
        }
    }

    fun setBindData(jsonObject: JSONObject?, onClick: IProofGhanaSelectListener?) {
        this.onItemClick = onClick
        this.bindData = jsonObject
        setTitle(jsonObject?.optString("title"))
        setInputHint(jsonObject?.optString("hint"))
        setInputText(jsonObject?.optString("content"))
        setDataList(
            jsonObject?.optJSONArray("values") ?: JSONArray(),
            input.text.toString()
        )
    }

    fun setTitle(title: String?) {
        this.title.text = title
    }

    fun setInputHint(hint: String?) {
        input.hint = hint
    }

    fun setInputText(text: String?) {
        input.setText(text)
    }

    private fun setDataList(jsonArray: JSONArray, checked: String) {

        dataList.apply {
            clear()
            for (index in 0 until jsonArray.length()) {
                val item = jsonArray[index] as JSONObject
                val title = item.optString("title")
                var didChecked = false
                if (checked.isNotBlank() and (title == checked)) {
                    didChecked = true
                }
                item.put("checked", didChecked)
                dataList.add(jsonArray[index] as JSONObject)
            }
        }
    }

    fun getInputText(): String {
        return input.text.toString()
    }
}

/**
 * adapter监听接口
 */
interface IProofGhanaSelectAdapterListener {
    fun onClick(position: Int, item: JSONObject)
}

/**
 * adapter
 */
class ProofGhanaSelectAdapter(
    private val dataList: List<JSONObject>,
    private val onItemClick: IProofGhanaSelectAdapterListener? = null
) :
    RecyclerView.Adapter<ProofGhanaSelectAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        private val title: TextView = view.findViewById(R.id.title)
        private val check: ImageView = view.findViewById(R.id.checked)
        private val container: RelativeLayout = view.findViewById(R.id.container)

        fun setItem(item: String, checked: Boolean) {
            title.text = item
            check.visibility = if (checked) View.VISIBLE else View.GONE
            if (checked) {
                check.visibility = View.VISIBLE
                title.setTextColor(Color.parseColor("#000000"))
                title.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
                container.setBackgroundColor(Color.parseColor("#1AB9B9B9"))
            } else {
                check.visibility = View.GONE
                title.setTextColor(Color.parseColor("#999999"))
                title.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
                container.setBackgroundColor(Color.parseColor("#FFFFFF"))
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.view_ghana_proof_select_adapter, parent, false)
        return ViewHolder(view)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item: JSONObject = dataList[position]
        holder.setItem(item.optString("title"), item.optBoolean("checked", false))
        holder.itemView.setOnClickListener {
            onItemClick?.onClick(position, item)
        }
    }

    override fun getItemCount(): Int {
        return dataList.size
    }
}
